package com.knowledge.graph.service.impl;

import com.knowledge.graph.mapper.*;
import com.knowledge.graph.model.entity.Course;
import com.knowledge.graph.model.entity.Relation;
import com.knowledge.graph.model.entity.Video;
import com.knowledge.graph.service.GraphService;
import com.knowledge.graph.model.vo.ResponseVO;
import com.knowledge.graph.model.vo.StatusCode;
import com.knowledge.graph.model.vo.VideoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiwenlong
 * @version 1.0
 * @description: 知识图谱业务操作
 * @date 2023/11/10 22:01
 */
@Service
public class GraphServiceImpl implements GraphService {
    @Autowired
    private RelationRepository relationRepository;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private EntityRepository entityRepository;

    @Autowired
    private NodeAndVideoMapper nodeAndVideoMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public ResponseVO getGraphByCourse(Integer courseID) {
        Course course = courseMapper.selectById(courseID);
        List<Relation> all = relationRepository.getByNodeType(course.getCourseNodeType());
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), all);
    }


    @Override
    public ResponseVO getGraphType() {
        List<String> entityType = entityRepository.selectAllType();
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), entityType);
    }

    @Override
    public ResponseVO addGraphType(String courseNodeType, Integer courseID) {
        Course course = courseMapper.selectById(courseID);
        course.setCourseNodeType(courseNodeType);
        courseMapper.updateById(course);
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
    }

    @Override
    public ResponseVO getGraphByLayers(String nodeName, String nodeType, Integer layers) {
        List<Relation> relations = relationRepository.getByNodeID(nodeName, nodeType, layers);
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), relations);
    }

    @Override
    public ResponseVO getVideoByNodeID(Integer nodeID) {
        int[] videoIDs = nodeAndVideoMapper.getVideoByNodeID(nodeID);
        List<VideoVO> videoVOS = new ArrayList<>();
        for (int videoID : videoIDs){
            Video video = videoMapper.selectById(videoID);
            videoVOS.add(new VideoVO(videoID,video.getVideoName()));
        }
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), videoVOS);
    }
}
